package mybatisdemo.test

import com.tsp.ssm.po.ItemsCustom
import com.tsp.ssm.po.User
import mybatisdemo.mapper.UserMapper
import mybatisdemo.mybatisfirst
import mybatisdemo.po.UserCustom
import mybatisdemo.po.UserQueryVo
import org.apache.ibatis.io.Resources
import org.apache.ibatis.session.SqlSession
import org.apache.ibatis.session.SqlSessionFactory
import org.apache.ibatis.session.SqlSessionFactoryBuilder
import org.junit.Test

/**
 * Created by 隼 on 2016/11/28.
 */
class UserMapperTest extends GroovyTestCase {
    private SqlSessionFactory sqlSessionFactory;
    // 此方法是在执行testFindUserById之前执行
    void setUp() {
        super.setUp()
        // 创建sqlSessionFactory
        //mybatis 配置文件
        String resource = "config/mybatis/sqlMaptest.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);

        //创建会话工厂,传入mybatis 配置文件
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);




    }


    void testFindUserById() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建UserMapper对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        //调用userMapper的方法
        User user =userMapper.findUserById(1);
        System.out.println(new mybatisfirst().UsertoString(user) );
        sqlSession.close();


    }

    void testFindUserByName() {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建UserMapper对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        //调用userMapper的方法
        List<User> list=userMapper.findUserByName("小明");
        sqlSession.close();
        Iterator<User> iterator=list.iterator();
        while (iterator.hasNext()){
            System.out.println(new mybatisfirst().UsertoString(iterator.next()) );
            
        }
    }

    void testInsertUser() {

    }

    void testDeleteUser() {

    }


    void testfindUserList(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建UserMapper对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        //创建包装对象，设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        userCustom.setSex("1");
        userCustom.setUsername("张三丰");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper的方法
        List<UserCustom> list=userMapper.findUserList(userQueryVo);
        List<UserCustom> list2=userMapper.findUserList(null);

        List<Integer> ids=new ArrayList<Integer>();
        ids.add(1);
        ids.add(10);
        ids.add(22);
        UserQueryVo userQueryVo_ids=new UserQueryVo();
        UserCustom userCustom_ids=new UserCustom();
        userQueryVo_ids.setIds(ids);
        userQueryVo_ids.setUserCustom(userCustom_ids);
        List<UserCustom> list3=userMapper.findUserList(userQueryVo_ids);

        Iterator<UserCustom> iterator=list.iterator();
        println ("list1");
        while(iterator.hasNext()){
            System.out.println(new mybatisfirst().UsertoString(iterator.next()));
        }
        println ("list2");
        iterator=list2.iterator()
        while(iterator.hasNext()){
            System.out.println(new mybatisfirst().UsertoString(iterator.next()));
        }
        println ("list3");
        iterator=list3.iterator()
        while(iterator.hasNext()){
            System.out.println(new mybatisfirst().UsertoString(iterator.next()));
        }
    }
    void testfindUserCount(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建UserMapper对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);

        //创建包装对象，设置查询条件
        UserQueryVo userQueryVo=new UserQueryVo();
        UserCustom userCustom=new UserCustom();
        userCustom.setSex("1");
        userCustom.setUsername("张三丰");
        userQueryVo.setUserCustom(userCustom);
        //调用userMapper的方法
        int count=userMapper.findUserCount(userQueryVo);
        int count2=userMapper.findUserCount(null);
        println ("count:  "+count+"    count2:  "+count2);
    }

    void  testfindUserByIdResultMap(){
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建UserMapper对象
        UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
        User user=userMapper.findUserByIdResultMap(1)
        System.out.println(new mybatisfirst().UsertoString(user));
    }
}
